package com.nanyang.tms.manager.bill.handler;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.bill.BillRecordDao;
import com.nanyang.tms.dao.waybill.WaybillReceiptDao;
import com.nanyang.tms.dao.waybill.WaybillShipmentDao;
import com.nanyang.tms.domain.bill.BillRecordDO;
import com.nanyang.tms.domain.bill.context.BillUpdateContext;
import com.nanyang.tms.domain.waybill.WaybillReceiptDO;
import com.nanyang.tms.domain.waybill.WaybillShipmentDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillSolveWaybillHandler extends AbstractTransHandler{
	@Autowired
	private WaybillReceiptDao waybillReceiptDao;
	@Autowired
	private WaybillShipmentDao waybillShipmentDao;
	@Autowired
	private BillRecordDao billRecordDao;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		//运单编码
		BillUpdateContext billUpdateContext = (BillUpdateContext)context.getInnerContext(BillUpdateContext.class);
		try {
			//收货
			receipt(billUpdateContext);
			//出货
			shipment(billUpdateContext);
			//已处理
			solveBillRecord(billUpdateContext);
			return true;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		

	}

	private void receipt(BillUpdateContext billUpdateContext){
		WaybillReceiptDO waybillReceiptDO = new WaybillReceiptDO();
		waybillReceiptDO.setWaybillCode(billUpdateContext.getWaybillCode());
		waybillReceiptDO.setSolveChargeWeight(billUpdateContext.getSolveChargeWeight());
		waybillReceiptDO.setSolveWeight(billUpdateContext.getSolveWeight());
		waybillReceiptDO.setSolveVolumeWeight(billUpdateContext.getSolveVolumeWeight());
		waybillReceiptDO.setGmtModifiedPin(billUpdateContext.getPin());
		waybillReceiptDao.updateByWayCode(waybillReceiptDO);

	}
	private void shipment(BillUpdateContext billUpdateContext){
		WaybillShipmentDO waybillShipmentDO = new WaybillShipmentDO();
		waybillShipmentDO.setWaybillCode(billUpdateContext.getWaybillCode());
		waybillShipmentDO.setSolveChargeWeight(billUpdateContext.getSolveChargeWeight());
		waybillShipmentDO.setSolveWeight(billUpdateContext.getSolveWeight());
		waybillShipmentDO.setSolveVolumeWeight(billUpdateContext.getSolveVolumeWeight());
		waybillShipmentDO.setGmtModifiedPin(billUpdateContext.getPin());
		waybillShipmentDO.setSupplierChargeWeight(billUpdateContext.getSolveChargeWeight());
		waybillShipmentDao.updateByWayCode(waybillShipmentDO);
	}

	private void solveBillRecord(BillUpdateContext billUpdateContext){
		BillRecordDO billRecordDO = new BillRecordDO();
		billRecordDO.setWaybillCode(billUpdateContext.getWaybillCode());
		billRecordDO.setGmtModifiedPin(billUpdateContext.getPin());
		billRecordDao.updateState(billRecordDO);
	}
}
